/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.dto;

import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.config.enums.DiscretionTypeEnum;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 分页查询事项裁量配置参数
 *
 * @author Junfeng.Chen
 * @date 2024/1/31 9:04
 **/
@Data
public class ConfigOrgRegulatoryDiscretionSaveDto {

    /**
     * 事项唯一id
     */
    @NotEmpty(message = "事项唯一id不能为空")
    private String regulatoryCatalogId;

    @NotNull(message = "启用标准不能为空")
    private DiscretionTypeEnum discretionType;

    /**
     * 个人违法情形配置集合
     */
    @Valid
    @NotNull(message = "个人违法配置不能为空可以为空数组")
    @Size(min = 0, max = 50, message = "违法情形配置最多允许配置10个")
    private List<ConfigOrgRegulatoryDiscretionRuleDto> personalRules;

    /**
     * 单位违法情形配置集合
     */
    @Valid
    @NotNull(message = "单位违法配置不能为空可以为空数组")
    @Size(min = 0, max = 50, message = "违法情形配置最多允许配置50个")
    private List<ConfigOrgRegulatoryDiscretionRuleDto> orgRules;

    public void check(List<String> punishTypes) {
        personalRules.forEach(ruleDto -> ConfigOrgRegulatoryDiscretionRuleDto.check(punishTypes, ruleDto));
        long personalRuleNames = personalRules.stream().map(ConfigOrgRegulatoryDiscretionRuleDto::getRuleName).distinct().count();
        if (personalRuleNames < personalRules.size()) {
            throw new BusinessException("个人违法情节中存在违法情节名称重复，请检查后重试");
        }
        orgRules.forEach(ruleDto -> ConfigOrgRegulatoryDiscretionRuleDto.check(punishTypes, ruleDto));
        long orgRuleNames = orgRules.stream().map(ConfigOrgRegulatoryDiscretionRuleDto::getRuleName).distinct().count();
        if (orgRuleNames < orgRules.size()) {
            throw new BusinessException("单位违法情节中存在违法情节名称重复，请检查后重试");
        }
    }
}